var e = require("../../@babel/runtime/helpers/interopRequireDefault")(require("../../@babel/runtime/regenerator")),
  t = require("../../@babel/runtime/helpers/asyncToGenerator"),
  n = getApp(),
  a = require("../../mybehaviors/getInvite/getInvite"),
  o = 2 * n.globalData.statusBarHeight + 40,
  i = wx.getMenuButtonBoundingClientRect().height;

Page({
  behaviors: [a],
  data: {
    statusBarHeight: o + i,
    yzredirect: "",
    logintap: !1,
    PlatformAgreement: "",
    openPlatformAgreement: !1,
    tapSelectAgreement: !0,
    agreement_name: "平台协议",
    canIUseGetUserProfile: !1,
    showAutoBindPhone: !1,
    showChangeID: !1,
    changidInfo: {
      uid: null,
      change_uid: null
    },
    AuthorizedPhone: "",
    balance: "余额",
    point: "积分"
  },
  onLoad: function (e) {
    this.data.yzredirect = n.getyzredirect(), wx.getUserProfile && this.setData({
      canIUseGetUserProfile: !0
    });
    try {
      var t = wx.getStorageSync("yz_basic_info");
      console.log(t), this.setData({
        agreement_name: n._isTextEmpty(t.home.register_setting.agreement_name) ? this.data.agreement_name : t.home.register_setting.agreement_name,
        PlatformAgreement: t.home.register_setting.new_agreement
      });
    } catch (e) {
      try {
        var a = wx.getStorageSync("new_agreement");
        this.setData({
          agreement_name: n._isTextEmpty(a.agreement_name) ? this.data.agreement_name : a.agreement_name,
          PlatformAgreement: a.new_agreement
        });
      } catch (e) {
        console.log(e);
      }
    }
  },
  PlatformAgreementTap: function () {
    this.setData({
      openPlatformAgreement: !0
    });
  },
  PlatformAgreementClose: function () {
    this.setData({
      openPlatformAgreement: !1
    });
  },
  PlatformAgreementOnChange: function (e) {
    this.setData({
      tapSelectAgreement: e.detail
    });
  },
  onReady: function () {},
  onShow: function () {},
  getUserProfileTap: function (e) {
    var t = this;
    //  return;
    if (!this.data.PlatformAgreement || this.data.tapSelectAgreement)
      if (this.data.logintap) console.log("正在请求");
      else if (this.setData({
        logintap: !0
      }), wx.showLoading({
        title: "登录中"
      }), this.data.canIUseGetUserProfile) wx.getUserProfile({
      desc: "获取用户信息",
      success: function (e) {
        var n = {
          nickName: e.userInfo.nickName,
          avatarUrl: "",
          gender: e.userInfo.gender
        };
        console.log("info:", n),
          wx.getLocation({
            type: 'gcj02', // 比较精确
            success: (res) => {
              console.log(res.latitude, res.longitude, '成功');
              let address = {
                lat: res.latitude,
                lon: res.longitude
              }
              t.setData({
                address: address
              })
              wx.setStorageSync('address', address)
              t.loginbtn(n);
            },
            fail: (res) => {
              console.log(res, '失败')
              wx.showToast({
                title: '请授权位置信息',
                icon: 'none'
              })
              wx.hideLoading();
              t.data.logintap = false
              wx.getSetting({
                success(res) {
                console.log(res)
                if (res.authSetting == true) {
                console.log('已授权')
                } else if (res.authSetting['scope.userLocation'] == false) {
                  wx.showModal({
                    title: '提示', //提示的标题,
                    content: '请打开位置权限', //提示的内容,
                    showCancel: true, //是否显示取消按钮,
                    cancelText: '取消', //取消按钮的文字，默认为取消，最多 4 个字符,
                    cancelColor: '#000000', //取消按钮的文字颜色,
                    confirmText: '确定', //确定按钮的文字，默认为取消，最多 4 个字符,
                    confirmColor: '#3CC51F', //确定按钮的文字颜色,
                    success: res => {
                      if (res.confirm) {
                        // openSetting 是需要事件驱动的，保证它的同步性。
                        wx.openSetting({
                          success(res) {
                            console.log(res.authSetting)
                            console.log('wei授权')
                          },
                          fail(res) {
                            console.log(res)
                            console.log('wei授权1')
                          }
                        })
                      } else if (res.cancel) {
                        console.log('用户点击取消')
                        wx.showToast({
                          title: '需要位置权限',
                          icon: 'none',
                          duration: 2000,
                        })
                      }
                    }
                  })
                }
              }
            })
              
              
            }

          })
      },
      fail: function (e) {
        console.log(e), wx.hideLoading({
          success: function (e) {}
        });
      }
    });
    else {
      var n = e.detail,
        a = {
          nickName: n.userInfo.nickName,
          avatarUrl: "",
          gender: n.userInfo.gender
        };
      console.log("info:兼容登录", a), this.loginbtn(a);
    } else wx.showToast({
      title: "请勾选平台协议",
      duration: 1e3,
      icon: "none"
    });

  },
  loginbtn: function (e) {
    // var t = this;
    console.log(this.data.logintap, this.data.address,"状态");
    var a = this;
    wx.login({
      success: function (o) {
        console.log(e, o.code, "login登录成功，获取用户info"), o.code ? wx.request({
          url: n.getNetAddresss("member.login.index"),
          header: {
            "Content-Type": "application/json",
            Accept: "application/json"
          },
          data: {
            code: o.code,
            info: e,
            ver: "2"
          },
          success: function (t) {
            setTimeout(function () {
              a.setData({
                logintap: !1
              });
            }, 1e3);
            var n = t.data;

            if (console.log("登录接口", t), 1 == n.result) try {
              n.data.user_info && wx.setStorageSync("nickname", n.data.user_info.nickname), n.data.session && wx.setStorageSync("session", n.data.session),
                n.data.openid && wx.setStorageSync("openid", n.data.openid), n.data.wx_token && wx.setStorageSync("wx_token", n.data.wx_token),
                e && wx.setStorageSync("userInfo", e), n.data.uid && wx.setStorageSync("yz_uid", n.data.uid),
                wx.hideLoading(), n.data.is_first_time_bind ? a.setData({
                  showAutoBindPhone: !0
                }) : a.getInviteData(), wx.removeStorageSync("new_decorate");
            } catch (e) {
              
              wx.reLaunch({
                    url: "/packageG/index/index"
                  });
            } else wx.hideLoading(), wx.showModal({
              title: "提示",
              content: n.msg,
              success: function (e) {
                console.log(e);
              }
            }), console.log("第三方登录数据错误" + t.errMsg);
          },
          fail: function (e) {
            wx.hideLoading(), wx.showToast({
              title: "第三方登录失败session、wx_token获取失败" + e.errMsg,
              duration: 3e3,
              icon: "none"
            }), setTimeout(function () {
              a.setData({
                logintap: !1
              });
            }, 1e3), console.log("第三方登录失败session、wx_token获取失败" + e.errMsg);
          }
        }) : (t.setData({
          logintap: !1
        }), wx.showToast({
          title: "获取用户登录态失败！" + res.errMsg,
          duration: 3e3,
          icon: "none"
        }), console.log("获取用户登录态失败！" + res.errMsg));
      },
      fail: function (e) {
        wx.hideLoading(), t.setData({
          logintap: !1
        }), console.log("登录失败，请求被拒绝！获取用户登录态失败！" + e);
      }
    });
  },
  registerXiaoNiu:function (id) {
    console.log('registerXiaoNiu')
    var e = this,
      t = n.getNetAddresss("HuanNiu.zhuce_xiaoniu");
    n._getNetWork({
      url: t,
      data: {
        uid: id
      },
      success: function (t) {
        var n = t.data;
        if (1 == n.result) {
          var a = n.data;
          console.log('registerXiaoNiu1',a)
          try {
            
          } catch (e) {
            
          }
        }
        wx.hideLoading();
      },
      fail: function (e) {
        console.log(e);
      }
    });
  },
  getInviteData: function () {
    console.log('走了么')
    let _this = this
    wx.request({
      url: n.getNetAddresss("HuanNiu.get_address"),
      header: {
        "Content-Type": "application/json",
        Accept: "application/json"
      },
      data: {
        latitude: _this.data.address.lat,
        longitude: _this.data.address.lon,
        uid: wx.getStorageSync("yz_uid")
      },
      success: function (t) {
        console.log(t,'HuanNiu.get_address')
        _this.getInvites();
      }
    })

    
  },
  getInvites: function () {
    var e = this,
      t = n.getNetAddresss("member.member.isValidatePage");
    n._getNetWork({
      url: t,
      data: {},
      success: function (t) {
        var n = t.data;
        if (1 == n.result) {
          var a = n.data;
          try {
            e.checktoInvite(a);
          } catch (e) {
            wx.reLaunch({
              url: "/packageG/index/index"
            });
          }
        }
        wx.hideLoading();
      },
      fail: function (e) {
        console.log(e);
      }
    });
  },
  initLanguage: function () {
    var e = n.getNetAddresss("setting.get-lang-setting");
    n._getNetWork({
      url: e,
      data: {},
      success: function (e) {
        var t = e.data;
        if (1 == t.result) {
          var n = t.data;
          try {
            wx.setStorageSync("mailLanguage", JSON.stringify(n));
          } catch (e) {
            console.log(e);
          }
        }
      },
      fail: function (e) {
        console.log(e);
      }
    });
  },
  gomember: function () {
    wx.reLaunch({
      url: "/packageG/member_v2/member_v2"
    });
  },
  getPhoneNumber: function (n) {
    var a = this;
    return t(e.default.mark(function t() {
      var o, i;
      return e.default.wrap(function (e) {
        for (;;) switch (e.prev = e.next) {
          case 0:
            if ("getPhoneNumber:ok" != (o = n.detail).errMsg) {
              e.next = 13;
              break;
            }
            return e.next = 4, a.getPhone_index(o.code);

          case 4:
            return i = e.sent, a.data.AuthorizedPhone = i, e.next = 8, a.getPrepBind(i);

          case 8:
            return e.next = 10, a.setBindPhone();

          case 10:
            a.getInviteData(), e.next = 14;
            break;

          case 13:
            a.getInviteData();

          case 14:
          case "end":
            return e.stop();
        }
      }, t);
    }))();
  },
  getPhoneM: function () {
    return new Promise(function (e, t) {
      wx.checkSession({
        success: function (t) {
          console.log(t, "checkSession"), e();
        },
        fail: function (e) {
          t(e);
        }
      });
    });
  },
  getSessionKey: function () {
    var e = wx.getStorageSync("session");
    console.log(e, "缓存的session");
    var t = n.getNetAddresss("member.mini-decrypt.getSessionKey");
    return new Promise(function (a, o) {
      n._postNetWork({
        url: t,
        data: {
          session_key_id: e,
          not_validate_page: "0"
        },
        showToastIn: !1,
        success: function (e) {
          var t = e.data;
          if (1 == t.result) {
            if (console.log(t, "获取session_key"), 1 == t.result) {
              var n = t.data.session_key_data.session_key;
              a(n);
            }
          } else wx.showToast({
            icon: "none",
            title: t.msg,
            duration: 1500
          }), o();
        },
        fail: function (e) {
          o(e);
        }
      });
    });
  },
  getPhone_index: function (e, t) {
    wx.showLoading({
      title: "加载中"
    });
    var a = n.getNetAddresss("member.member.miniMemberMobile");
    return new Promise(function (t, o) {
      n._postNetWork({
        url: a,
        showToastIn: !1,
        data: {
          code: e,
          not_validate_page: "0"
        },
        success: function (e) {
          var n = e.data;
          1 == n.result ? t(n.data.phone_info.purePhoneNumber) : (wx.hideLoading({
            success: function (e) {}
          }), wx.showToast({
            icon: "none",
            title: n.msg,
            duration: 1500
          }), o());
        },
        fail: function (e) {
          o(e);
        }
      });
    });
  },
  setBindPhone: function (e) {
    var t = this;
    wx.hideLoading({
      success: function (e) {}
    });
    var a = n.getNetAddresss("member.member.bindMobile");
    return new Promise(function (e, o) {
      n._postNetWork({
        url: a,
        data: {
          mobile: t.data.AuthorizedPhone,
          mini_first_time_bind: 1
        },
        success: function (n) {
          var a = n.data;
          t.data.changidInfo && t.data.changidInfo.change_uid && wx.setStorage({
            key: "yz_uid",
            data: t.data.changidInfo.change_uid
          }), wx.showToast({
            icon: "none",
            title: a.msg,
            duration: 1500
          }), t.getInviteData(), e();
        },
        fail: function (e) {
          o(e);
        }
      });
    });
  },
  closeChangeId: function () {
    this.setData({
      showChangeID: !1
    });
  },
  getPrepBind: function (e) {
    var t = this,
      a = n.getNetAddresss("member.member.prepBind");
    return new Promise(function (o, i) {
      n._postNetWork({
        url: a,
        showToastIn: !1,
        data: {
          mobile: e
        },
        validateBol: 2,
        success: function (e) {
          var n = e.data;
          if (1 == n.result)
            if (n.data.is_show) {
              wx.hideLoading({
                success: function (e) {}
              });
              var a = wx.getStorageSync("yz_basic_info");
              t.setData({
                balance: a.balance,
                point: a.point,
                showChangeID: !0,
                changidInfo: {
                  uid: n.data.uid,
                  change_uid: n.data.change_uid
                }
              });
            } else o();
          else wx.showToast({
            icon: "none",
            title: n.msg,
            duration: 1500
          }), i(n);
        },
        fail: function (e) {
          i(e);
        }
      });
    });
  }
});